package com.q.tqb.view.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.provider.SyncStateContract;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.q.tqb.R;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.ShareInfo;
import com.q.tqb.util.BitmapUtil;
import com.q.tqb.util.DensityUtil;
import com.q.tqb.util.LogUtil;
import com.q.tqb.view.SocializeImageView;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.uuzuche.lib_zxing.activity.CodeUtils;

public class SharePopWindow extends PopupWindow implements OnClickListener {

    private View popView;

    private Context mContext;
    private Bitmap mBitmap;
    private ShareInfo mShareInfo;
    private int mTargetScene;
    private IWXAPI api;

    public SharePopWindow(Context context, ShareInfo shareInfo, Bitmap bitmap) {
        this.mContext = context;
        this.mShareInfo = shareInfo;
        this.mBitmap = bitmap;
        init();
    }

    private void init() {
        popView = View.inflate(mContext, R.layout.pop_share, null);
        api = WXAPIFactory.createWXAPI(mContext, AppConst.WEIXIN_APP_ID);
        SocializeImageView iv_wechat = popView.findViewById(R.id.iv_wechat);
        SocializeImageView iv_wxcircl = popView.findViewById(R.id.iv_wxcircl);
        SocializeImageView iv_fav = popView.findViewById(R.id.iv_fav);
        TextView share_cancel = popView.findViewById(R.id.share_cancel);

        iv_wechat.setOnClickListener(this);
        iv_wxcircl.setOnClickListener(this);
        share_cancel.setOnClickListener(this);
        iv_fav.setOnClickListener(this);

        //把View添加到PopWindow中
        this.setContentView(popView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.share_pop_anim);

        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);


        popView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = popView.findViewById(R.id.ll_pop_share).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.iv_wechat:
                mTargetScene = SendMessageToWX.Req.WXSceneSession;
                share();

                dismiss();
                break;

            case R.id.iv_wxcircl:
                mTargetScene = SendMessageToWX.Req.WXSceneTimeline;
                share();

                dismiss();
                break;
            case R.id.iv_fav:
                mTargetScene = SendMessageToWX.Req.WXSceneFavorite;
                share();

                dismiss();
                break;
            case R.id.share_cancel:

                dismiss();
                break;
        }
    }

    private void share() {
        if (mShareInfo.getType() == 2) {
    //        if (mTargetScene == SendMessageToWX.Req.WXSceneFavorite || mTargetScene == SendMessageToWX.Req.WXSceneTimeline) {
                WXWebpageObject webpage = new WXWebpageObject();
                webpage.webpageUrl = mShareInfo.getUrl();
                WXMediaMessage msg = new WXMediaMessage(webpage);
                msg.title = mShareInfo.getShare_title();
                msg.description = mShareInfo.getShare_desc();
                Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_push_logo);
                Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
                msg.thumbData = BitmapUtil.bitmapBytes(thumbBmp);
                bmp.recycle();
                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("webpage");
                req.message = msg;
                req.scene = mTargetScene;
                api.sendReq(req);
//            } else {
//                if (TextUtils.isEmpty(mShareInfo.getRedDetailInfo().getPacket_picture())) {
//                    WXMiniProgramObject miniProgram = new WXMiniProgramObject();
//                    miniProgram.webpageUrl = "https://m.tqboo.com";
//                    miniProgram.userName = "gh_186e1c4b573c";
//                    miniProgram.path = "pages/detail/index?rid=" + mShareInfo.getRedDetailInfo().getRid();
//                    WXMediaMessage msg = new WXMediaMessage(miniProgram);
//                    msg.title = mShareInfo.getRedDetailInfo().getNickname() + "给你发红包了!!";
//                    Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_mini);
//
//                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
//                    msg.thumbData = BitmapUtil.bitmapBytes(thumbBmp);
//
//                    SendMessageToWX.Req req = new SendMessageToWX.Req();
//                    req.transaction = buildTransaction("webpage");
//                    req.message = msg;
//                    req.scene = SendMessageToWX.Req.WXSceneTimeline;
//                    api.sendReq(req);
//                } else {
//                    String[] images = mShareInfo.getRedDetailInfo().getPacket_picture().split(";");
//                    Glide
//                            .with(mContext)
//                            .load(images[0])
//                            .asBitmap()
//                            .into(target);
//                }

   //         }
        } else if (mShareInfo.getType() == 1) {
            WXImageObject imgObj = new WXImageObject(mBitmap);
            WXMediaMessage msg = new WXMediaMessage();
            msg.mediaObject = imgObj;
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("img");
            req.message = msg;
            req.scene = mTargetScene;
            api.sendReq(req);
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }


    private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(150, 150) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {

            WXMiniProgramObject miniProgram = new WXMiniProgramObject();
            miniProgram.webpageUrl = "https://m.tqboo.com";
            miniProgram.userName = "gh_186e1c4b573c";
            miniProgram.path = "pages/detail/index?rid=" + mShareInfo.getRedDetailInfo().getRid();
            WXMediaMessage msg = new WXMediaMessage(miniProgram);
            msg.title = mShareInfo.getRedDetailInfo().getNickname() + "给你发红包了!!";
            msg.thumbData = BitmapUtil.bitmapBytes1(bitmap);
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("webpage");
            req.message = msg;
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
            api.sendReq(req);
        }

    };

}